﻿Imports System.Drawing
Imports System.Windows.Forms

Friend Class cWindow
    Implements ISaver, IWindow

    Public Sub New()

    End Sub

    'Values for fullscreen
    Private Const SM_CXSCREEN As Integer = 0
    Private Const SM_CYSCREEN As Integer = 1
    Private Shared HWND_TOP As IntPtr = IntPtr.Zero
    Private Const SWP_SHOWWINDOW As Integer = 64

    Private Declare Function SetWindowPos Lib "user32.dll" Alias "SetWindowPos" _
    (ByVal hWnd As IntPtr, ByVal hWndIntertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
    Private Declare Function GetSystemMetrics Lib "user32.dll" Alias "GetSystemMetrics" (ByVal Which As Integer) As Integer

    Public ReadOnly Property ScreenX() As Integer
        Get
            Return GetSystemMetrics(SM_CXSCREEN)
        End Get
    End Property

    Public ReadOnly Property ScreenY() As Integer
        Get
            Return GetSystemMetrics(SM_CYSCREEN)
        End Get
    End Property


    Public Sub SaveSizeAndLocation(ByVal f As Form) Implements ISaver.SaveSizeAndLocation

        If f.WindowState.ToString = "Normal" Then
            SaveSetting(Application.ProductName, "FormPositions", f.Name & "_Top", f.Top.ToString)
            SaveSetting(Application.ProductName, "FormPositions", f.Name & "_Left", f.Left.ToString)
            SaveSetting(Application.ProductName, "FormPositions", f.Name & "_Height", f.Height.ToString)
            SaveSetting(Application.ProductName, "FormPositions", f.Name & "_Width", f.Width.ToString)
            SaveSetting(Application.ProductName, "FormPositions", f.Name & "_WindowState", CStr(0))
        End If
        If f.WindowState.ToString = "Maximized" Then
            SaveSetting(Application.ProductName, "FormPositions", f.Name & "_WindowState", CStr(2))
        End If

    End Sub

    Public Sub GetSizeAndLocation(ByVal f As Form) Implements ISaver.GetSizeAndLocation
        f.Top = _
            CInt(GetSetting(Application.ProductName, "FormPositions", f.Name & "_Top", f.Top.ToString))
        f.Left = _
            CInt(GetSetting(Application.ProductName, "FormPositions", f.Name & "_Left", f.Left.ToString))
        f.Height = _
            CInt(GetSetting(Application.ProductName, "FormPositions", f.Name & "_Height", f.Height.ToString))
        f.Width = _
            CInt(GetSetting(Application.ProductName, "FormPositions", f.Name & "_Width", f.Width.ToString))
        f.WindowState = _
            CType(GetSetting(Application.ProductName, "FormPositions", f.Name & "_WindowState", CStr(0)), FormWindowState)
    End Sub

    Public Sub SetFullScreen(ByVal win As Form) Implements IWindow.SetFullScreen
        Try
            win.WindowState = FormWindowState.Maximized
            win.FormBorderStyle = FormBorderStyle.None
            win.TopMost = True
            SetWindowPos(win.Handle, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW)
            win.Show()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub ExitFullScreenWindow(ByVal win As Form) Implements IWindow.ExitFullScreenWindow
        Try
            win.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

End Class
